import api from "../../api/index";
const dayjs = require("dayjs");
// pages/statistics/statistics.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 0,

    explainShow: false,
    shopId: null,
    shopData: {
      name: "shop_id",
      list: [],
    },

    dayData: null,
    monthDate: null,
    contrastData: null,
    isLoading: false,

    show: false,
    dateForm: [
      {
        label: "日同比",
        value: "day",
      },
      {
        label: "月同比",
        value: "month",
      },
    ],
    dataType: 0,
    actDate: "day",
    showDate: false,
    dates: [],
    formatterDate: (e, v) => {
      return v + (e === "day" ? "日" : e === "month" ? "月" : "年");
    },
    currentDate: null,

    explainShow: false,
    explainData: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      shopId: wx.getStorageSync("shopID"),
      isLoading: true,
    });

    Promise.all([
      this.getDayData(),
      this.getMonthData(),
      this.getShopList(),
    ]).then((res) => {
      this.setData({
        isLoading: false,
      });
    });
  },
  getShopList() {
    return new Promise((resolve, reject) => {
      api.apiStoreShopList().then((res) => {
        this.setData({
          "shopData.list": res.data.list,
        });
        resolve();
      });
    });
  },
  getShopValue(e) {
    this.setData({
      shopId: e.detail.value,
      isLoading: true,
    });
    let arr = [this.getDayData(), this.getMonthData()];
    if (this.data.type) arr.push(this.getContrastData());
    Promise.all(arr).then((res) => {
      this.setData({
        isLoading: false,
      });
    });
  },
  getDayData(e) {
    return new Promise((resolve, reject) => {
      if (e) {
        wx.showLoading({
          title: "刷新中...",
          mask: true,
        });
      }
      let { shopId } = this.data;
      api
        .apiStoreStatistical({
          shop_id: shopId,
        })
        .then((res) => {
          if (shopId !== this.data.shopId) return;
          let member = res.data.member_statistical;
          let course = res.data.course_statistical;
          let dayData = {
            updateTime: res.data.update_time,
            member: {
              listA: [
                {
                  label: "办卡(张)",
                  num: member.card_count,
                  changeType: member.card_count_change_type,
                  changeDiff: member.card_count_diff,
                  changeValue: member.card_count_diff_value + "张",
                  span: 3,
                },
                {
                  label: "金额(元)",
                  num: Number(member.card_cost).toFixed(2),
                  changeType: member.card_cost_change_type,
                  changeDiff: member.card_cost_diff,
                  changeValue:
                    Number(member.card_cost_diff_value).toFixed(2) + "元",
                  span: 3,
                },
                {
                  label: "到店(人次)",
                  num: member.store_personnel,
                  changeType: member.store_personnel_change_type,
                  changeDiff: member.store_personnel_diff,
                  changeValue: member.store_personnel_diff_value + "人",
                },
                {
                  label: "门店会员(人次)",
                  num: member.shop_member_count,
                  changeType: member.shop_member_count_change_type,
                  changeDiff: member.shop_member_count_diff,
                  changeValue: member.shop_member_count_diff_value + "人",
                },
                {
                  label: "平台会员(人次)",
                  num: member.platform_member,
                  changeType: member.platform_member_change_type,
                  changeDiff: member.platform_member_diff,
                  changeValue: member.platform_member_diff_value + "人次",
                },
              ],
              listB: [
                {
                  label: "有效会员(人)",
                  num: member.member_valid,
                },
                {
                  label: "未启用卡(人)",
                  num: member.my_card_unopen,
                },
                {
                  label: "即将到期(人)",
                  num: member.my_card_will_expire,
                },
              ],
            },
            course: {
              listA: [
                {
                  label: "售课(课)",
                  num: course.course_count,
                  changeType: course.course_count_change_type,
                  changeDiff: course.course_count_diff,
                  changeValue: course.course_count_diff_value + "课",
                },
                {
                  label: "课节数(节)",
                  num: course.course_classes_count,
                  changeType: course.course_classes_count_change_type,
                  changeDiff: course.course_classes_count_diff,
                  changeValue: course.course_classes_count_diff_value + "节",
                },
                {
                  label: "金额(元)",
                  num: Number(course.course_cost).toFixed(2),
                  changeType: course.course_cost_change_type,
                  changeDiff: course.course_cost_diff,
                  changeValue:
                    Number(course.course_cost_diff_value).toFixed(2) + "元",
                },
                {
                  label: "上课(人)",
                  num: course.classes_member_count,
                  changeType: course.classes_member_count_change_type,
                  changeDiff: course.classes_member_count_diff,
                  changeValue: course.classes_member_count_diff_value + "人",
                },
                {
                  label: "上课节数(节)",
                  num: course.classes_count,
                  changeType: course.classes_count_change_type,
                  changeDiff: course.classes_count_diff,
                  changeValue: course.classes_count_diff_value + "节",
                },
                {
                  label: "上课金额(元)",
                  num: Number(course.classes_cost).toFixed(2),
                  changeType: course.classes_cost_change_type,
                  changeDiff: course.classes_cost_diff,
                  changeValue:
                    Number(course.classes_cost_diff_value).toFixed(2) + "元",
                },
              ],
              listB: [
                {
                  label: "进行中(课)",
                  num: course.classes_doing_course_count,
                },
                {
                  label: "进行中课节(节)",
                  num: course.classes_doing_classes_count,
                },
                {
                  label: "进行中金额(元)",
                  num: Number(course.classes_doing_cost).toFixed(2),
                },
                {
                  label: "完成中(课)",
                  num: course.course_finish_count,
                },
                {
                  label: "完成中课节(节)",
                  num: course.course_finish_classes_count,
                },
                {
                  label: "完成中金额(元)",
                  num: Number(course.course_finish_cost).toFixed(2),
                },
                {
                  label: "即将到期(课)",
                  num: course.will_expire_courses_count,
                },
                {
                  label: "将到期中课节(节)",
                  num: course.will_expire_courses_classes_count,
                },
                {
                  label: "将到期中金额(元)",
                  num: Number(course.will_expire_courses_cost).toFixed(2),
                },
              ],
            },
          };
          this.setData({
            dayData,
          });
          resolve();
        })
        .finally((_) => {
          if (e) {
            wx.hideLoading();
          }
        });
    });
  },
  getMonthData(e) {
    return new Promise((resolve, reject) => {
      if (e) {
        wx.showLoading({
          title: "刷新中...",
          mask: true,
        });
      }
      let { shopId } = this.data;
      api
        .apiStoreMonthStatistical({
          shop_id: shopId,
        })
        .then((res) => {
          if (shopId !== this.data.shopId) return;
          let member = res.data.member_statistical;
          let course = res.data.course_statistical;
          let monthDate = {
            updateTime: res.data.update_time,
            member: {
              listA: [
                {
                  label: "办卡(张)",
                  num: member.card_count,
                  changeType: member.card_count_change_type,
                  changeDiff: member.card_count_diff,
                  changeValue: member.card_count_diff_value + "张",
                  span: 3,
                },
                {
                  label: "金额(元)",
                  num: Number(member.card_cost).toFixed(2),
                  changeType: member.card_cost_change_type,
                  changeDiff: member.card_cost_diff,
                  changeValue:
                    Number(member.card_cost_diff_value).toFixed(2) + "元",
                  span: 3,
                },
                {
                  label: "到店(人次)",
                  num: member.store_personnel,
                  changeType: member.store_personnel_change_type,
                  changeDiff: member.store_personnel_diff,
                  changeValue: member.store_personnel_diff_value + "人",
                },
                {
                  label: "门店会员(人次)",
                  num: member.shop_member_count,
                  changeType: member.shop_member_count_change_type,
                  changeDiff: member.shop_member_count_diff,
                  changeValue: member.shop_member_count_diff_value + "人",
                },
                {
                  label: "平台会员(人次)",
                  num: member.platform_member,
                  changeType: member.platform_member_change_type,
                  changeDiff: member.platform_member_diff,
                  changeValue: member.platform_member_diff_value + "人次",
                },
              ],
            },
            course: {
              listA: [
                {
                  label: "售课(课)",
                  num: course.course_count,
                  changeType: course.course_count_change_type,
                  changeDiff: course.course_count_diff,
                  changeValue: course.course_count_diff_value + "课",
                },
                {
                  label: "课节数(节)",
                  num: course.course_classes_count,
                  changeType: course.course_classes_count_change_type,
                  changeDiff: course.course_classes_count_diff,
                  changeValue: course.course_classes_count_diff_value + "节",
                },
                {
                  label: "金额(元)",
                  num: Number(course.course_cost).toFixed(2),
                  changeType: course.course_cost_change_type,
                  changeDiff: course.course_cost_diff,
                  changeValue:
                    Number(course.course_cost_diff_value).toFixed(2) + "元",
                },
                {
                  label: "上课(人)",
                  num: course.classes_member_count,
                  changeType: course.classes_member_count_change_type,
                  changeDiff: course.classes_member_count_diff,
                  changeValue: course.classes_member_count_diff_value + "人",
                },
                {
                  label: "上课节数(节)",
                  num: course.classes_count,
                  changeType: course.classes_count_change_type,
                  changeDiff: course.classes_count_diff,
                  changeValue: course.classes_count_diff_value + "节",
                },
                {
                  label: "上课金额(元)",
                  num: Number(course.classes_cost).toFixed(2),
                  changeType: course.classes_cost_change_type,
                  changeDiff: course.classes_cost_diff,
                  changeValue:
                    Number(course.classes_cost_diff_value).toFixed(2) + "元",
                },
              ],
            },
          };
          this.setData({
            monthDate,
          });
          resolve();
        })
        .finally((_) => {
          if (e) {
            wx.hideLoading();
          }
        });
    });
  },
  getContrastData() {
    return new Promise((resolve, reject) => {
      let { shopId, dates, actDate } = this.data;
      api
        .apiStoreStatisticalCompare({
          shop_id: shopId,
          compare_form: actDate,
          left_date: dates[0],
          right_date: dates[1],
        })
        .then((res) => {
          if (shopId !== this.data.shopId) return;
          let data = res.data.compare_data;
          let contrastData = [
            {
              label: "总营业额",
              left: "￥" + Number(data.shop_cost_left).toFixed(2),
              right: "￥" + Number(data.shop_cost_right).toFixed(2),
              diff: data.shop_cost_diff,
              diffType: data.shop_cost_change_type,
            },
            {
              label: "会卡业额",
              left: "￥" + Number(data.card_cost_left).toFixed(2),
              right: "￥" + Number(data.card_cost_right).toFixed(2),
              diff: data.card_cost_diff,
              diffType: data.card_cost_change_type,
            },
            {
              label: "会卡数量",
              left: data.card_count_left,
              right: data.card_count_right,
              diff: data.card_count_diff,
              diffType: data.card_count_change_type,
            },
            {
              label: "课程业额",
              left: "￥" + Number(data.course_cost_left).toFixed(2),
              right: "￥" + Number(data.course_cost_right).toFixed(2),
              diff: data.course_cost_diff,
              diffType: data.course_cost_change_type,
            },
            {
              label: "课程数量",
              left: data.course_count_left,
              right: data.course_count_right,
              diff: data.course_count_diff,
              diffType: data.course_count_change_type,
            },
            {
              label: "到店",
              left: data.store_personnel_count_left,
              right: data.store_personnel_count_right,
              diff: data.store_personnel_count_diff,
              diffType: data.store_personnel_count_change_type,
            },
            {
              label: "门店会员",
              left: data.shop_member_count_left,
              right: data.shop_member_count_right,
              diff: data.shop_member_count_diff,
              diffType: data.shop_member_count_change_type,
            },
            {
              label: "平台会员",
              left: data.platform_member_count_left,
              right: data.platform_member_count_right,
              diff: data.platform_member_count_diff,
              diffType: data.platform_member_change_type,
            },
            {
              label: "上课(课)",
              left: data.classes_course_count_left,
              right: data.classes_course_count_right,
              diff: data.classes_course_count_diff,
              diffType: data.classes_course_count_change_type,
            },
            {
              label: "上课(节)",
              left: data.classes_count_left,
              right: data.classes_count_right,
              diff: data.classes_count_diff,
              diffType: data.classes_count_change_type,
            },
            {
              label: "上课金额",
              left: "￥" + Number(data.classes_cost_left).toFixed(2),
              right: "￥" + Number(data.classes_cost_right).toFixed(2),
              diff: data.classes_cost_diff,
              diffType: data.classes_cost_change_type,
            },
          ];
          this.setData({ contrastData });
          resolve();
        });
    });
  },

  onFooterBtn() {
    let { type } = this.data;
    if (!type) {
      this.setData({
        show: true,
        actDate: "day",
        dates: [],
      });
    } else {
      this.setData({
        type: 0,
      });
    }
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  dateChonge(e) {
    this.setData({
      actDate: e.currentTarget.dataset.id,
      dates: [],
    });
  },
  openDate(e) {
    let type = e.currentTarget.dataset.type;
    let date = +dayjs(this.data.dates[type] || dayjs().startOf("day"));
    this.setData({
      showDate: true,
      currentDate: date,
      dataType: type,
    });
  },
  onCloseDate() {
    this.setData({
      showDate: false,
    });
  },
  getDateVal(e) {
    this.setData({
      currentDate: e.detail,
    });
  },
  async getDate() {
    let { show, dataType, currentDate, dates, actDate } = this.data;
    dates[dataType] = dayjs(currentDate).format(
      actDate === "day" ? "YYYY-MM-DD" : "YYYY-MM"
    );
    this.setData({
      dates,
      showDate: false,
    });
    if (!show) {
      wx.showLoading({
        title: "刷新中...",
        mask: true,
      });
      await this.getContrastData();
      wx.hideLoading();
    }
  },
  async getDateValue() {
    let { dates } = this.data;
    if (!dates[0] || !dates[1]) {
      wx.showToast({
        title: "请选择时间段",
        icon: "none",
      });
      return;
    }
    this.setData({
      type: 1,
      show: false,
      isLoading: true,
    });
    await this.getContrastData();
    this.setData({
      isLoading: false,
    });
  },

  explainClose() {
    this.setData({
      explainShow: false,
    });
  },
  explainOpen(e) {
    let i = e.currentTarget.dataset.type;
    let explainData = {};
    switch (+i) {
      case 0:
        explainData = {
          title: "今日会员说明",
          content: "对今日会员各项数据同比昨日涨跌比、涨跌值进行对比",
        };
        break;
      case 1:
        explainData = {
          title: "今日课程说明",
          content: "对今日课程各项数据同比昨日涨跌比、涨跌值进行对比",
        };
        break;
      case 2:
        explainData = {
          title: "本月会员说明",
          content: "对本月会员各项数据同比昨日涨跌比、涨跌值进行对比",
        };
        break;
      case 3:
        explainData = {
          title: "本月课程说明",
          content: "对本月课程各项数据同比昨日涨跌比、涨跌值进行对比",
        };
        break;
      case 4:
        explainData = {
          title: "数据统计同比",
          content:
            "主要是为了对比两个时间内的涨跌比，现在主要是日同比和月同比。\n日同比：不同日期的对比\n月同比：不同月份的对比",
        };
        break;
    }
    this.setData({
      explainData,
      explainShow: true,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
